/*
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/29 12:14
 */
package org.mechanic.core.web;

import org.springframework.context.annotation.PropertySource;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * 处理输出信息
 *
 * @author mechanic
 * @version 0.0.1
 */
@PropertySource(value = "classpath:i18n/message.perproties")
public final class Resources {
    /** 国际化信息 */
    private static final Map<String, ResourceBundle> MESSAGES = new HashMap<String, ResourceBundle>();

    /** 国际化信息 */
    public static String getMessage(String key, Object... params) {
        Locale locale = Locale.CHINA;
        ResourceBundle message = MESSAGES.get(locale.getLanguage());
        if (message == null) {
            synchronized (MESSAGES) {
                message = MESSAGES.computeIfAbsent(locale.getLanguage(),
                                                   k -> ResourceBundle.getBundle("i18n/message")
                );
            }
        }
        if (params != null && params.length > 0) {
            return String.format(message.getString(key), params);
        }
        return message.getString(key);
    }

    /** 清除国际化信息 */
    public static void flushMessage() {
        MESSAGES.clear();
    }
}
